package com.nec.recommend.config;

import com.nec.recommend.common.Result;
import org.springframework.core.MethodParameter;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.net.URI;
import java.util.List;

@RestControllerAdvice
public class ResponseEntityProcess implements ResponseBodyAdvice<Object>, Ordered {

    private final List<String> ignoreList = List.of("/v3/api-docs", "/v3/api-docs/swagger-config", "/v2/api-docs", "/swagger-resources");


    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        URI uri = request.getURI();
        if (body instanceof Result || ignoreList.contains(uri.getPath())) {
            return body;
        }
        return Result.success(body);
    }


    @Override
    public int getOrder() {
        return 0;
    }
}
